package com.baiyang.app.ui.bookshelf.workList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.baiyang.app.HomeActivity;
import com.baiyang.app.R;
import com.baiyang.app.databinding.ActivityWorkListBinding;
import com.baiyang.app.model.Article;
import com.baiyang.app.tool.LoadDataScrollController;
import com.baiyang.app.tool.MyAdapter;
import com.baiyang.app.ui.read.ReadPageActivity;

import java.util.ArrayList;

public class WorkListActivity extends AppCompatActivity implements LoadDataScrollController.OnRecycleRefreshListener {

    private String authorId;

    private ActivityWorkListBinding binding;

    private WorkListViewModel viewModel;
    private WorkListService service;
    private RecyclerView recyclerView;
    //下拉刷新
    private SwipeRefreshLayout mSwipeRefresh;
    private LoadDataScrollController mController;
    private MyAdapter<Article> myAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_work_list);
        viewModel = new ViewModelProvider(this).get(WorkListViewModel.class);
        service = new WorkListService(getApplicationContext(),viewModel);
        authorId = getIntent().getStringExtra("authorId");
        initHandler();

        recyclerView = binding.recyclerView;
        mSwipeRefresh = binding.refresh;
        this.mController = new LoadDataScrollController(this);
        myAdapter = new MyAdapter<Article>(new ArrayList<>(),R.layout.item_article) {
            @Override
            public void bindView(ViewHolder holder, Article obj) {
                holder.setPic(R.id.article_pic,obj.getCoverPic());
                holder.setText(R.id.introduction,obj.getIntroduction());
                holder.setText(R.id.name,obj.getArticleName());
                holder.setText(R.id.article_type,obj.getTypesName());
                holder.setText(R.id.reader_num,obj.getReaderNum().toString());
                holder.setmOnItemClickListener(e->{
                    Intent intent = new Intent();
                    intent.putExtra("articleId",obj.getId());
                    intent.setClass(getApplicationContext(), ReadPageActivity.class);
                    getApplicationContext().startActivity(intent);
                    overridePendingTransition(R.anim.left_in,R.anim.right_out);
                });
            }
        };
        //设置垂直的线性布局管理器，Orientation --> VERTICAL:垂直 HORIZONTAL:水平
        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        //添加分割线
//        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
        this.mSwipeRefresh.setColorSchemeColors(Color.RED,Color.GREEN,Color.BLUE);
        this.mSwipeRefresh.setOnRefreshListener(mController);
        this.recyclerView.addOnScrollListener(mController);
    }

    private void initHandler() {
        viewModel.getArticleResult().observe(this,e->{
            String result = e.getString("result");
            if("SUCCESS".equals(result)){
                JSONArray list = e.getJSONArray("list");
                if (list != null && list.size() > 0) {
                    myAdapter.addAll(list.toJavaList(Article.class));
                }else{
                    Toast.makeText(getApplicationContext(), "没有更多了", Toast.LENGTH_SHORT).show();
                }
            }else{
                String detail = e.getString("detail");
                Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        myAdapter.clear();
        service.getArticle(authorId);
    }

    @Override
    public void refresh() {
        //刷新的接口调
        mSwipeRefresh.postDelayed(new Runnable() {
            @Override
            public void run() {
                myAdapter.clear();
                service.getArticle(authorId);
                mSwipeRefresh.setRefreshing(false);
                mController.setLoadDataStatus(false);
            }
        },2000);
    }

    @Override
    public void loadMore() {

    }
}